import 'package:json_annotation/json_annotation.dart';

part 'user_model.g.dart';

/// u7528u6237u6a21u578b
@JsonSerializable()
class UserModel {
  /// u7528u6237ID
  final String id;

  /// u7528u6237u540du79f0
  final String name;

  /// u7528u6237u5934u50cf
  final String? avatar;

  /// u624bu673au53f7
  final String? phone;
  
  /// u90aeu7bb1
  final String? email;

  /// u5faeu4fe1u7528u6237ID
  final String? wxOpenId;

  /// u521bu5efau65f6u95f4
  @JsonKey(name: 'created_at')
  final DateTime? createdAt;

  /// u6784u9020u51fdu6570
  UserModel({
    required this.id,
    required this.name,
    this.avatar,
    this.phone,
    this.email,
    this.wxOpenId,
    this.createdAt,
  });

  /// u4ece JSON u521bu5efau5bf9u8c61
  factory UserModel.fromJson(Map<String, dynamic> json) => UserModel(
        id: json['id'] as String,
        name: json['name'] as String,
        avatar: json['avatar'] as String?,
        phone: json['phone'] as String?,
        email: json['email'] as String?,
        wxOpenId: json['wx_open_id'] as String?,
        createdAt: json['created_at'] != null
            ? DateTime.parse(json['created_at'] as String)
            : null,
      );

  /// u8f6cu6362u4e3a JSON
  Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'avatar': avatar,
        'phone': phone,
        'email': email,
        'wx_open_id': wxOpenId,
        'created_at': createdAt?.toIso8601String(),
      };

  /// u521bu5efau7a7au7528u6237
  factory UserModel.empty() => UserModel(
        id: '',
        name: '',
      );

  /// u590du5236u5e76u66f4u65b0u5c5eu6027
  UserModel copyWith({
    String? id,
    String? name,
    String? avatar,
    String? phone,
    String? email,
    String? wxOpenId,
    DateTime? createdAt,
  }) {
    return UserModel(
      id: id ?? this.id,
      name: name ?? this.name,
      avatar: avatar ?? this.avatar,
      phone: phone ?? this.phone,
      email: email ?? this.email,
      wxOpenId: wxOpenId ?? this.wxOpenId,
      createdAt: createdAt ?? this.createdAt,
    );
  }
}
